package com.si.controller.manage;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.si.service.Login_Err_LogService;
import com.si.util.UserOperate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.si.entity.SysUser;
import com.si.service.InfoService;
import com.si.service.SysUserService;


/**
 * @author 蒋世维
 * 
 * 项目名称：gardens
 * 类名称：IndexController
 * 类描述：登录验证
 * 创建人：jiangshiwei
 * 创建时间：2018-12-27 PM12:59:08
 * 修改备注：
 * @version  1.0.0
 */
@Controller
@RequestMapping("login")
public class LoginManageController {
	
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private InfoService infoService;
	@Autowired
	private Login_Err_LogService login_err_logService;
	/**
	 * 进入到登录页面
	 * @return
	 */
	@RequestMapping
	public String load() {
		return "managePage/login";
	}
	
	/**
	 * 
	 * @return
	 */
	@RequestMapping(value="verification")

	public String verification(
			HttpServletRequest request,
			ModelMap modelMap,
			@RequestParam(value = "userName",required = false) String userName,
			@RequestParam(value = "passWord",required = false) String passWord) {
		Integer code = sysUserService.loginSysUser(userName,passWord,request);
		if (code==0){
			return "redirect:/login/main.html";
		}else if(code==1){
			return "redirect:/login/fail.html";
		}else if(code==2){
			return "redirect:/login/failerr.html";
		}else{
			return "redirect:/login/failerr.html";
		}
	}
	
	/**
	 * 登录成功后跳转到主页
	 * @return
	 */
	@UserOperate(module = "系统登录",methods = "系统登录-用户登录")
	@RequestMapping(value = "main")
	public String main(
			HttpServletRequest request,ModelMap modelMap,HttpServletResponse response) {
		//统计每个分类下文章的数量
		String sql="SELECT" + 
					"	count(t_info.id)," + 
					"	t_info.info_type_id," + 
					"	t_info_type_classa.name" + 
					"	FROM" + 
					"	t_info" + 
					"	INNER JOIN t_info_type_classa ON t_info_type_classa.id = t_info.info_type_id" + 
					"	GROUP BY info_type_id";
		List<Object[]> infoCountList=infoService.createSqlQuery(sql);
		modelMap.put("infoCountList", infoCountList);
		return "managePage/main";
	}
	
	/**
	 * 登录失败后挑战到登录页面
	 * @return
	 */
	@RequestMapping(value = "fail")
	public String fail(ModelMap modelMap) {
		modelMap.put("login_failure", "账户被锁定请30分钟后尝试");
		return "managePage/login";
	}

	/**
	 * 登录失败后挑战到登录页面
	 * @return
	 */
	@RequestMapping(value = "failerr")
	public String failerr(ModelMap modelMap) {
		modelMap.put("login_failure", "用户名或密码错误");
		return "managePage/login";
	}
	/**
	 * 退出登录
	 * @return
	 */
	@RequestMapping(value = "logout")
	@UserOperate(module = "系统",methods = "系统功能-用户退出")
	public String logout(ModelMap modelMap,HttpServletRequest request) {
		request.getSession().removeAttribute("session_sysUser");

		return "redirect:/login.html";
	}
}
